<style>
    .layui-table-cell {
        height: 40px;
        line-height: 40px;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">采购入库计划编号：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="planNo" class="layui-input" type="text"
                                               placeholder="请输入采购入库计划编号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                            lay-submit layui-form-keyDownSearch="true">查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置
                                    </button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="purchaseWarehousingPlanBtnAdd" class="layui-btn icon-btn color-reseda">添加
                            </button>
                        </div>
                        <div class="lay-btn-rt"></div>
                    </div>
                </div>
            </div>

            <!-- 表格 -->
            <table class="layui-table" id="purchaseWarehousingPlanTable" lay-filter="purchaseWarehousingPlanTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="purchaseWarehousingPlanTableBar">
    <a class="layui-btn layui-btn-xs" lay-event="view">详情</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="purchaseWarehousingPlanForm">
    <form lay-filter="purchaseWarehousingPlanForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">运单编号</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <select id="purchaseDeliveryRecordId" name="purchaseDeliveryRecordId" lay-filter="purchaseDeliveryRecordId"></select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">物流发货记录号</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" id="logisticsNo" name="logisticsNo" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">所属采购订单</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" id="purchaseOrderNo" name="purchaseOrderNo" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">供应商</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" id="corporateName" name="corporateName" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">供应商联系人</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" id="businessContact" name="businessContact" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">采购部门</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" id="departmentName" name="departmentName" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">采购申请人</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" id="purchaseApplicant" name="purchaseApplicant" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">订单审核人</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" id="auditorName" name="auditorName" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">运输方式</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" id="transportModelName" name="transportModelName" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">车牌号</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" id="plateNo" name="plateNo" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">车辆联系电话</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" id="phone" name="phone" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">入库仓库类型</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <select id="warehouseType" name="warehouseType" lay-filter="warehouseType"></select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">入库仓库</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <select id="warehouse" name="warehouse" lay-filter="warehouse"></select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div style="margin-left: 30px;">
                <div class="layui-btn" id="addProduct" style="margin-top: 10px;">添加入库计划物料</div>
                <div id="productList">
                    <table class="layui-table" id="purchaseDeliveryRecordProductTable" lay-filter="purchaseDeliveryRecordProductTable"></table>
                </div>
            </div>
        </div>

        <div class="layui-form-item text-center" style="margin-top: 50px;">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="purchaseWarehousingPlanFormSubmit" lay-submit>提交</button>
        </div>
    </form>
</script>

<!--入库物料-->
<script type="text/html" id="purchaseDeliveryRecordProductList">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <div class="layui-form lay-header">
                    <div class="toolbar">
                        <div class="layui-form-item lay-query-content">
                            <div class="lay-query-box">
                                <div class="query-lt">
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">物料编号：</label>
                                        <div class="layui-input-inline mr0">
                                            <input name="skuCode" class="layui-input" type="text"
                                                   placeholder="请输入物料编号"/>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">物料名称：</label>
                                        <div class="layui-input-inline mr0">
                                            <input name="productName" class="layui-input" type="text"
                                                   placeholder="请输入物料名称"/>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <button class="layui-btn icon-btn color-green"
                                                lay-filter="deliveryRecordProductFormSubSearchLog" lay-submit>查询
                                        </button>
                                        <button class="layui-btn icon-btn color-white"
                                                lay-filter="deliveryRecordProductResets" lay-submit>重置
                                        </button>
                                        <button class="layui-btn layui-btn-normal icon-btn" id="confirmChoose">
                                            确认选择
                                        </button>
                                    </div>
                                </div>
                                <div class="query-rt"></div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- 表格 -->
                <table class="layui-table" id="purchaseDeliveryRecordProductListTable"
                       lay-filter="purchaseDeliveryRecordProductListTable"></table>
            </div>
        </div>
    </div>
</script>

<!--签收数量(按基础单位)-->
<script type="text/html" id="quantity">
    <div>
        <input name="quantity" placeholder="请输入入库数量" type="text" lay-verify="required|quantity|compare" required
                class="layui-input" maxlength="10" style="text-align: center;"/>
    </div>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="purchaseDeliveryRecordProductTableBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<!-- 详情弹窗 -->
<script type="text/html" id="purchaseWarehousingPlanView">
    <form lay-filter="purchaseWarehousingPlanView" class="layui-form model-form">
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">运单编号</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="waybillNo" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">物流发货记录号</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="logisticsNo" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">所属采购订单</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="sn" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">供应商</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="corporateName" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">供应商联系人</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="businessContact" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">采购部门</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="departmentName" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">采购申请人</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="purchaseApplicant" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">订单审核人</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="auditorName" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">运输方式</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="transportModeDesc" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">车牌号</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="plateNo" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">车辆联系电话</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="phone" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">入库仓库类型</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" id="warehouseTypeName" name="warehouseTypeName" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">入库仓库</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" id="warehouseName" name="warehouseName" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div style="margin-left: 30px;">
                <table class="layui-table" id="purchaseDeliveryRecordProductDetailTable" lay-filter="purchaseDeliveryRecordProductDetailTable"></table>
            </div>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#purchaseWarehousingPlanTable',
            url: config.base + 'enterprise/purchase/admin/purchaseWarehousingPlan/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers',title: "序号", width: 60}
                , {field: 'planNo', align: 'center', title: '采购入库计划编号', width: 180}
                , {field: 'sn', align: 'center', title: '采购单编号', width: 180}
                , {field: 'skuNum', align: 'center', title: '入库计划数'}
                , {field: 'corporateName', align: 'center', title: '供应商', width: 200}
                , {field: 'totalAmount', align: 'center', title: '金额'}
                , {field: 'statusDesc', align: 'center', title: '状态'}
                , {field: 'creatorName', align: 'center', title: '创建人'}
                , {field: 'createTime', align: 'center', title: '创建时间', width: 170}
                , {align: 'center', toolbar: '#purchaseWarehousingPlanTableBar', title: '操作', width: 150}
            ]],
            done: function (res, curr, count) {
            }
        });

        // 工具条点击事件
        table.on('tool(purchaseWarehousingPlanTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'view') { // 查看
                showViewModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(purchaseWarehousingPlanTable)', function (obj) {
            table.reload('purchaseWarehousingPlanTable', {
                initSort: obj,
                where: {
                    sort: obj.field,
                    order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#purchaseWarehousingPlanBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '80%',
                offset: ['120px', '300px'],
                title: data ? '修改采购入库计划' : '添加采购入库计划',
                content: $('#purchaseWarehousingPlanForm').html(),
                success: function () {
                    form.verify({
                        quantity: [/^[0-9]*(\.[0-9]{1,2})?$/, '格式错误'],
                        compare: function (data, item) {
                            var quantity = Number($(item).parent().parent().parent().prev().find("div").text()); // 剩余入库数
                            if (quantity < Number(data)) {
                                return '入库数量不能大于剩余入库数！';
                            }
                        }
                    });

                    function setData(res, purchaseDeliveryRecordId) {
                        var logisticsNo = "";
                        var purchaseOrderNo = "";
                        var corporateName = "";
                        var businessContact = "";
                        var departmentName = "";
                        var purchaseApplicant = "";
                        var auditorName = "";
                        var transportModelName = "";
                        var plateNo = "";
                        var phone = "";
                        if (purchaseDeliveryRecordId != "") {
                            $.each(res.data, function (index, item) {
                                if (item.id == purchaseDeliveryRecordId) {
                                    logisticsNo = item.logisticsNo;
                                    purchaseOrderNo = item.sn;
                                    corporateName = item.corporateName;
                                    businessContact = item.businessContact;
                                    departmentName = item.departmentName;
                                    purchaseApplicant = item.creatorName;
                                    auditorName = item.auditorName;
                                    transportModelName = item.transportModeDesc;
                                    plateNo = item.plateNo;
                                    phone = item.phone;
                                }
                            })
                        }
                        $("#logisticsNo").val(logisticsNo);
                        $("#purchaseOrderNo").val(purchaseOrderNo);
                        $("#corporateName").val(corporateName);
                        $("#businessContact").val(businessContact);
                        $("#departmentName").val(departmentName);
                        $("#purchaseApplicant").val(purchaseApplicant);
                        $("#auditorName").val(auditorName);
                        $("#transportModelName").val(transportModelName);
                        $("#plateNo").val(plateNo);
                        $("#phone").val(phone);
                    }

                    var codeArr = [];
                    var deliveryRecordProductList = [];

                    // 获取供应商发货记录
                    admin.req('enterprise/purchase/admin/purchaseDeliveryRecord/list', {
                        limit: 99999,
                        filterWarehousingComplete: 1
                    }, function (res) {
                        if (200 == res.code) {
                            $("form select[name='purchaseDeliveryRecordId']").append(new Option("请选择运单编号", ""));//往下拉菜单里添加元素
                            $.each(res.data, function (index, item) {
                                $("form select[name='purchaseDeliveryRecordId']").append(new Option(item.waybillNo, item.id));//往下拉菜单里添加元素
                            })
                            form.render(); //菜单渲染 把内容加载进去
                            if (data && data.purchaseDeliveryRecordId) {
                                form.val('purchaseSignForForm', data);
                                setData(res, data.purchaseDeliveryRecordId);
                            }
                            form.on("select(purchaseDeliveryRecordId)", function (d) {
                                $("#productList").find("tbody").remove();
                                codeArr = [];
                                setData(res, d.value);
                            })
                        } else {
                            layer.msg('获取供应商发货记录失败!', {icon: 2, time: 1000});
                        }
                    }, 'GET');

                    //入库仓库类型
                    admin.initDataSelect('enterprisecab/warehouse/admin/warehouseType/list', {limit: 9999}, "warehouseType", data ? data.warehouseType : null, "入库仓库类型", null, null);
                    //入库仓库-数据回显
                    if(data){
                        admin.initDataSelect('enterprisecab/warehouse/admin/warehouse/list', {
                            limit: 9999,
                            warehouseTypeId: data.warehouseType
                        }, "warehouse", data ? data.warehouse : null, "入库仓库", null, null);
                    }
                    //入库仓库
                    form.on('select(warehouseType)', function (selectData) {
                        $('#warehouse').empty();
                        admin.initDataSelect('enterprisecab/warehouse/admin/warehouse/list', {
                            limit: 9999,
                            warehouseTypeId: selectData.value
                        }, "warehouse", null, "入库仓库", null, null);
                    });

                    // 添加入库计划物料
                    $("#addProduct").click(function () {
                        var purchaseDeliveryRecordId = $("form select[name='purchaseDeliveryRecordId']").val();
                        if (purchaseDeliveryRecordId) {
                            var purchaseDeliveryRecordProductList = admin.open({
                                type: 1,
                                area: ['80%','600px'],
                                offset: ['160px', '285px'],
                                title: '选择签收物料',
                                content: $('#purchaseDeliveryRecordProductList').html(),
                                success: function () {
                                    var codes = "";
                                    for (var i = 0; i < codeArr.length; i++) {
                                        if (i == 0) {
                                            codes = '"' + codeArr[i] + '"';
                                        } else {
                                            codes += "," + '"' + codeArr[i] + '"';
                                        }
                                    }

                                    // 渲染表格
                                    var insTb = table.render({
                                        elem: '#purchaseDeliveryRecordProductListTable',
                                        url: config.base + 'enterprise/purchase/admin/purchaseDeliveryRecordProduct/list',
                                        where: {
                                            purchaseDeliveryRecordId: purchaseDeliveryRecordId,
                                            filterWarehousingComplete: 1,
                                            codes: codes
                                        },
                                        page: false,
                                        cellMinWidth: 100,
                                        cols: [[
                                            {type: 'checkbox'},
                                            {type: 'numbers', title: '序号', width: 50},
                                            {field: 'skuCode', align: 'center', title: '物料编号', width: 140},
                                            {field: 'productName', align: 'center', title: '物料名称', width: 140},
                                            {field: 'specifications', align: 'center', title: '规格型号', width: 140},
                                            {field: 'quantity', align: 'center', title: '数量'},
                                            {field: 'unitName', align: 'center', title: '计量单位', width: 100},
                                            {field: 'rate', align: 'center', title: '税率(%)'},
                                            {field: 'taxPrice', align: 'center', title: '含税单价'},
                                            {field: 'price', align: 'center', title: '单价'},
                                            {field: 'amount', align: 'center', title: '金额'},
                                            {field: 'tax', align: 'center', title: '税额'},
                                            {field: 'totalAmount', align: 'center', title: '合计金额'}
                                        ]]
                                    });

                                    // 搜索
                                    form.on('submit(deliveryRecordProductFormSubSearchLog)', function (data) {
                                        insTb.reload({where: data.field, page: {curr: 1}}, 'data');
                                    });

                                    // 重置搜索
                                    form.on('submit(deliveryRecordProductResets)', function (data) {
                                        $(".lay-header").find('select').val("");
                                        $(".lay-header").find('input').val("");
                                        let field = data.field;
                                        for (let key in field) {
                                            field[key] = ''
                                        }
                                        insTb.reload({where: field, page: {curr: 1}}, 'data');
                                    });

                                    // 确认选择
                                    $('#confirmChoose').click(function () {
                                        var checkStatus = table.checkStatus('purchaseDeliveryRecordProductListTable'); //test即为基础参数id对应的值
                                        var dataS = checkStatus.data;
                                        for (var i = 0; i < dataS.length; i++) {
                                            dataS[i].purchaseDeliveryRecordProductId = dataS[i].id;
                                        }
                                        if (dataS.length == 0) {
                                            layer.msg("请选择物料", {icon: 2, time: 1000});
                                        } else {
                                            for (var i = 0; i < dataS.length; i++) {
                                                deliveryRecordProductList.push(dataS[i]);
                                                codeArr.push(dataS[i].skuCode);
                                            }
                                            layer.close(purchaseDeliveryRecordProductList);

                                            // 获取数据集合并传入表格中
                                            table.reload('purchaseDeliveryRecordProductTable', {
                                                data: deliveryRecordProductList,
                                                limit: deliveryRecordProductList.length
                                            });
                                        }
                                    });
                                }
                            });
                        } else {
                            layer.msg('请选择运单编号!', {icon: 2, time: 1000});
                        }
                    })

                    // 渲染表格
                    var insTb = table.render({
                        elem: '#purchaseDeliveryRecordProductTable',
                        data: [deliveryRecordProductList],
                        page: false,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'numbers', height: 50}
                            , {field: 'purchaseDeliveryRecordProductId', title: ' 供应商发货记录ID', hide: 'true'}
                            , {field: 'productTypeId', align:'center', title: '物料类型ID', hide: 'true'}
                            , {
                                field: 'productPic', align:'center', title: '图片', templet: function (d) {
                                    return '<img src="' + d.productPic + '" style="height: 40px;" id="platformPic"/>'
                                }
                            }
                            , {field: 'skuCode', align:'center', title: '物料编号', width: 140}
                            , {field: 'specifications', align:'center', title: '规格号', width: 140}
                            , {field: 'productTypeName', align:'center', title: '物料类型'}
                            , {field: 'unitName', align:'center', title: '计价单位'}
                            , {field: 'purchaseQuantity', align:'center', title: '采购数量'}
                            , {field: 'quantity', align:'center', title: '发货数量'}
                            , {field: 'hasWarehousingQuantity', align:'center', title: '已入库数'}
                            , {
                                field: 'surplusWarehousingQuantity', align:'center', title: '剩余入库数', templet: function (d) {
                                    return d.quantity - d.hasWarehousingQuantity;
                                }
                            }
                            , {field: 'warehousingQuantity', align:'center', title: '入库数量', width: 150, templet: '#quantity'}
                            , {
                                align: 'center',
                                toolbar: '#purchaseDeliveryRecordProductTableBar',
                                title: '操作',
                                width: 100,
                                fixed: 'right'
                            }
                        ]]
                    });

                    form.val('purchaseWarehousingPlanForm', data);
                    // 表单提交事件
                    form.on('submit(purchaseWarehousingPlanFormSubmit)', function (d) {
                        var purchaseWarehousingPlanProducts = [];
                        $("#productList").find("tbody tr").each(function (index, item) {
                            var purchaseWarehousingPlanProduct = {};
                            if ($(item).find("td").length > 1) {
                                $(item).find("td").each(function (tdIndex, tdItem) {
                                    if ($(tdItem).data("field") == "purchaseDeliveryRecordProductId") {
                                        purchaseWarehousingPlanProduct.purchaseDeliveryRecordProductId = $(tdItem).find("div").text();
                                    }
                                    if ($(tdItem).data("field") == "warehousingQuantity") {
                                        purchaseWarehousingPlanProduct.quantity = $(tdItem).find("input").val();
                                    }
                                })
                                purchaseWarehousingPlanProducts.push(purchaseWarehousingPlanProduct);
                            }
                        })
                        if (purchaseWarehousingPlanProducts.length > 0) {
                            layer.load(2);
                            d.field.purchaseWarehousingPlanProducts = purchaseWarehousingPlanProducts;
                            admin.req(data ? 'enterprise/purchase/admin/purchaseWarehousingPlan/update' : 'enterprise/purchase/admin/purchaseWarehousingPlan/add', JSON.stringify(d.field), function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    table.reload('purchaseWarehousingPlanTable');
                                    layer.closeAll('page');
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, data ? 'PUT' : 'POST', false, "application/json");
                        } else {
                            layer.msg("至少添加一条入库物料！", {icon: 2});
                        }
                        return false;
                    });
                }
            });
        }

        // 显示详情弹窗
        function showViewModel(data) {
            admin.open({
                type: 1,
                area: ['80%','700px'],
                offset: ['150px','300px'],
                title: '采购入库计划详情',
                content: $('#purchaseWarehousingPlanView').html(),
                success: function () {
                    //数据回显
                    form.val("purchaseWarehousingPlanView",data);
                    // 渲染表格
                    table.render({
                        elem: '#purchaseDeliveryRecordProductDetailTable',
                        url: config.base + 'enterprise/purchase/admin/purchaseWarehousingPlanProduct/list',
                        where: {purchaseWarehousingPlanId: data.id},
                        page: false,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'numbers',title: "序号", height: 50}
                            , {
                                field: 'productPic', align:'center', title: '物料图片', templet: function (d) {
                                    return '<img src="' + d.productPic + '" style="height: 40px;" id="platformPic"/>'
                                }
                            }
                            , {field: 'skuCode', align:'center', title: '物料编码', width: 140}
                            , {field: 'specifications', align:'center', title: '规格号', width: 180}
                            , {field: 'productTypeName', align:'center', title: '物料类型'}
                            , {field: 'unitName', align:'center', title: '计价单位'}
                            , {field: 'purchaseQuantity', align:'center', title: '采购数量'}
                            , {field: 'deliveryQuantity', align:'center', title: '发货数量'}
                            , {field: 'quantity', align:'center', title: '入库数量', width: 140}
                        ]]
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/purchase/admin/purchaseWarehousingPlan/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // 导出excel
        $('#purchaseWarehousingPlanBtnExport').click(function () {
            var checkRows = table.checkStatus('purchaseWarehousingPlanTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>